package cn.star.jots.subject.service.impl;

import cn.star.jots.filmSubject.mapper.FSMapper;
import cn.star.jots.subject.mapper.SubjectMapper;
import cn.star.jots.subject.pojo.dto.SubjectQuery;
import cn.star.jots.subject.pojo.dto.SubjectSaveParam;
import cn.star.jots.subject.pojo.entity.Subject;
import cn.star.jots.subject.pojo.vo.SubjectVO;
import cn.star.jots.subject.service.SubjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    SubjectMapper subjectMapper;
    @Autowired
    FSMapper fsMapper;
    @Override
    public List<SubjectVO> selectAll(SubjectQuery subjectQuery) {
        log.debug("查询专题,参数:{}", subjectQuery);
        return subjectMapper.selectAll(subjectQuery);
    }

    @Override
    public void save(SubjectSaveParam subjectSaveParam) {
        Subject subject = new Subject();
        BeanUtils.copyProperties(subjectSaveParam, subject);
        if(subject.getId()==null){
            subject.setCreateTime(new Date());
            subjectMapper.insert(subject);
        } else {
            subject.setUpdateTime(new Date());
            subjectMapper.update(subject);
        }

    }

    @Override
    public void deleteById(Long id) {
        log.debug("删除专题,参数:{}", id);
        subjectMapper.deleteById(id);

        fsMapper.deleteBySubjectId(id);
    }
}
